#!/bin/bash
#
# extract inventory items from the mysql dump and convert to CSV.
# (c) bitmech.com, see LICENSE

#
# mysql dump file location
DUMP=tyr101-mysql5-v1.sql

#
# output csv file location
OUTBYID=items-by-id.csv     # item name by id
OUTBYVOL=items-by-vol.csv   # item id by id by volume
OUTBYMAT=items-by-mat.csv   # item id by material by quantity (no key)
OUTBYBAT=items-by-bat.csv   # item id by material by batch size 

#
# extract & transform...

#
# ...name by id:
OUT="${OUTBYID}"
echo -en "Nom nom nom... "
cat $DUMP                                     \
    | grep "INSERT INTO \`invTypes\`"         \
    | cut -c32-                               \
    | sed "s/, //g"                           \
    | awk -F, '{print $1","$3}'               \
    > ${OUT} 2>/dev/null
echo "done! Output is ${OUT}."

#
# ...name by id by vol
OUT="${OUTBYVOL}"
echo -en "Nom nom nom... "
cat $DUMP                                     \
    | grep "INSERT INTO \`invTypes\`"         \
    | cut -c32-                               \
    | sed "s/, //g"                           \
    | awk -F, '{print $1","$3","$8}'          \
    > ${OUT} 2>/dev/null
echo "done! Output is ${OUT}."

#
# ...id by material (no key)
OUT="${OUTBYMAT}"
echo -en "Nom nom nom... "
cat $DUMP                                     \
    | grep "INSERT INTO \`invTypeMaterials\`" \
    | cut -c39-                               \
    | sed 's/[();]//g'                        \
    | awk -F, '{print $1","$2","$3}'          \
    > ${OUT} 2>/dev/null
echo "done! Output is ${OUT}."

#
# ...id by batch size 
OUT="${OUTBYBAT}"
echo -en "Nom nom nom... "
cat $DUMP                                     \
    | grep "INSERT INTO \`invTypes\`"         \
    | cut -c32-                               \
    | sed "s/, //g"                           \
    | awk -F, '{print $1","$10}'              \
    > ${OUT} 2>/dev/null
echo "done! Output is ${OUT}."
